/**
 * 线路规划协议工具
 */
import ajaxAsync from '../../../utils/ajaxAsync'
import SimpleRoute from '../models/simple-route'
import XiaobuStorage from '../../../utils/XiaobuStorage';
import { routeCollection } from '../../public/confs/StorageKeys';
/**
 * 获取线路规划结果
 *
 * @param {string} startLon 开始的经度
 * @param {string} startLat 开始的纬度
 * @param {string} endLon 结束的经度
 * @param {string} endLat 结束的纬度
 // * @param {number} queryNumber 查询的条数
 * @param {number} type 查询的类型（1、少换乘<默认>；2、少站点）
 * @returns
 */
function getRoutes(
  startLon,
  startLat,
  endLon,
  endLat,
  // queryNumber,
  type
) {
  // return mockData()
  return ajaxAsync('/bus/line/latitudelongitude', {
    START_LONGITUDE: startLon,
    START_LATITUDE: startLat,
    END_LONGITUDE: endLon,
    END_LATITUDE: endLat,
    // QUERY_NUMBER: queryNumber,
    LINE_TYPE: type
  }).then(res => {
    let body = res
    if (body) {
      XiaobuStorage.setItem(routeCollection, JSON.stringify(body.LIST || []))
      let length = body.COUNT
      let list = body.LIST ? body.LIST : []
      list = list.map((item, index) => {
        let leftinfo = ''
        let rightinfo = `共${item.ALL_STOP_NUMBER}站`
        if (item.LINE_PRICE > 0) {
          rightinfo += `·票价${item.LINE_PRICE / 100}元`
        }
        let leftStationCount = item.STATION_COUNT
        if (leftStationCount === 0) {
          leftinfo = `${item.BUSNAMES[0]}·即将到站`
        } else if (leftStationCount === -99) {
          leftinfo = `${item.BUSNAMES[0]}·已经到站`
        } else if (leftStationCount < 0) {
          leftinfo = '暂无车辆信息'
        } else if (leftStationCount > 0) {
          leftinfo = `${item.BUSNAMES[0]}·距离${leftStationCount}站`
        }
        return new SimpleRoute(index, item.BUSNAMES, leftinfo, rightinfo)
      })
      return {
        list,
        length
      }
    }
  })
}
export { getRoutes }